const crypto = require('crypto');

// 生成 AES 加密密钥
const aesKey = crypto.randomBytes(32);

// 生成 AES 向量
const iv = crypto.randomBytes(16);

// 加密函数
function aesEncrypt(ctx, data) {
  const cipher = crypto.createCipheriv('aes-128-cbc', ctx.state.aesKey, ctx.state.iv);
  let encrypted = cipher.update(data, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  return encrypted;
}

// 解密函数
function aesDecrypt(ctx, data) {
  const decipher = crypto.createDecipheriv('aes-128-cbc', ctx.state.aesKey, ctx.state.iv);
  let decrypted = decipher.update(data, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}

module.exports = {
    aesEncrypt,
    aesDecrypt
}

